import Qt.labs.folderlistmodel 2.15
import QtQuick 2.15

Item {
    id: fileIO

    property string source: "qrc:/example.text"
    property string text: ""
    property bool error: false
    property string errorString: ""

    function read() {
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function() {
            if (xhr.readyState === XMLHttpRequest.DONE) {  // 添加状态检查
                if (xhr.status === 200) {
                    text = xhr.responseText;
                    error = false;
                    errorString = "";
                } else {
                    error = true;
                    errorString = "无法读取文件: " + source;
                    text = "";
                }
            }
        };
        xhr.open("GET", source);
        xhr.send();
    }

    function write() {
        console.log("写入文件需要后端支持");
    }
}
